package cn.ekuma.data.ui.android.editor;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import android.content.Context;
import android.util.AttributeSet;

import cn.ekuma.data.dao.ChildEditorRecord;
import cn.ekuma.data.dao.I_ParentNode;
import cn.ekuma.data.dao.bean.IKeyed;

public abstract class AbstractChildParentDAOJEditor <T extends IKeyed,K> extends AbstractDAOJEditor<T> implements I_ParentNode<K>,ChildEditorRecord<T>,ParentNodeChangeListener<K>{

    public AbstractChildParentDAOJEditor(DirtyManager dirty, Context context,
			AttributeSet attrs) {
		super(dirty, context, attrs);
	}

	protected List<ParentNodeChangeListener<K>> listeners = new ArrayList();

    protected I_ParentNode<K> parentNode;
    protected int parentState;
    
    public final void addObjectChangeListener(ParentNodeChangeListener<K> l) {
        listeners.add(l);
    }

    public final void removeListDataListener(ParentNodeChangeListener<K> l) {
        listeners.remove(l);
    }

    @Override
    public void writeValueInsert(Properties prop) {
        super.writeValueInsert(prop);
        for(ParentNodeChangeListener l:listeners)
            l.parentNodeValueChange(state, this);
    }

    public void writeValueDelete(T value) {
       super.writeValueDelete(value);
       for(ParentNodeChangeListener l:listeners)
            l.parentNodeValueChange(state, this);
    }

    public void writeValueEOF() {
       super.writeValueEOF();
       for(ParentNodeChangeListener l:listeners)
            l.parentNodeValueChange(state, this);
    }

    public void writeValueEdit(T value) {
       super.writeValueEdit(value);
       for(ParentNodeChangeListener l:listeners)
            l.parentNodeValueChange(state, this);
    }


    public IKeyed<K> getParentKey() {
        return obj;
    }

    public Class getParentClass() {
        return getEditClass();
    }

    public I_ParentNode<K> getParentNode() {
        return parentNode;
    }

    public void parentNodeValueChange(int state, I_ParentNode<K> parent) {
        this.parentState=state;
        this.parentNode=parent;
    }
    
    public void setParentNode(I_ParentNode<K> parentNode) {
        this.parentNode = parentNode;
    }

    public int getParentState() {
        return parentState;
    }

    public void setParentState(int parentState) {
        this.parentState = parentState;
    }

}